package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.ProTask;
import com.ruoyi.system.domain.ProTaskTemp;
import com.ruoyi.system.domain.vo.ProSubjectVo;
import com.ruoyi.system.domain.vo.ProTaskTempVo;
import com.ruoyi.system.domain.vo.ProTaskVo;
import com.ruoyi.system.mapper.ProTaskMapper;
import com.ruoyi.system.mapper.ProTaskTempMapper;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.ProSubjectTempBo;
import com.ruoyi.system.domain.vo.ProSubjectTempVo;
import com.ruoyi.system.domain.ProSubjectTemp;
import com.ruoyi.system.mapper.ProSubjectTempMapper;
import com.ruoyi.system.service.IProSubjectTempService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目管理模板Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-20
 */
@RequiredArgsConstructor
@Service
public class ProSubjectTempServiceImpl implements IProSubjectTempService {

    private final ProSubjectTempMapper baseMapper;

    private final ISysUserService sysUserService;

    private final ProTaskTempMapper proTaskTempMapper;
    /**
     * 查询项目管理模板
     */
    @Override
    public ProSubjectTempVo queryById(Long subjectId){
        ProSubjectTempVo proSubjectTempVo = baseMapper.selectVoById(subjectId);
        proSubjectTempVo.setHeadUsers(this.parseUsersBynames(proSubjectTempVo.getHeadUser()));
        proSubjectTempVo.setCopyUsers(this.parseUsersBynames(proSubjectTempVo.getCopyUser()));
        return proSubjectTempVo;
    }

    /**
     * 查询项目管理模板列表
     */
    @Override
    public TableDataInfo<ProSubjectTempVo> queryPageList(ProSubjectTempBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProSubjectTemp> lqw = buildQueryWrapper(bo);
        Page<ProSubjectTempVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(item->{
            item.setHeadUsers(this.parseUsersBynames(item.getHeadUser()));
            item.setCopyUsers(this.parseUsersBynames(item.getCopyUser()));
            List<ProTaskTempVo> proTaskVos = proTaskTempMapper.selectVoList(new LambdaQueryWrapper<ProTaskTemp>().eq(ProTaskTemp::getSubjectId, item.getSubjectId()));
            item.setTaskList(proTaskVos);
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目管理模板列表
     */
    @Override
    public List<ProSubjectTempVo> queryList(ProSubjectTempBo bo) {
        LambdaQueryWrapper<ProSubjectTemp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProSubjectTemp> buildQueryWrapper(ProSubjectTempBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProSubjectTemp> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectNo()), ProSubjectTemp::getSubjectNo, bo.getSubjectNo());
        lqw.eq(bo.getDdate() != null, ProSubjectTemp::getDdate, bo.getDdate());
        lqw.like(StringUtils.isNotBlank(bo.getSubjectName()), ProSubjectTemp::getSubjectName, bo.getSubjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getDesp()), ProSubjectTemp::getDesp, bo.getDesp());
        lqw.eq(bo.getFireDate() != null, ProSubjectTemp::getFireDate, bo.getFireDate());
        lqw.eq(bo.getDemandDate() != null, ProSubjectTemp::getDemandDate, bo.getDemandDate());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ProSubjectTemp::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCopyUser()), ProSubjectTemp::getCopyUser, bo.getCopyUser());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadUser()), ProSubjectTemp::getHeadUser, bo.getHeadUser());
        return lqw;
    }

    /**
     * 新增项目管理模板
     */
    @Override
    public Boolean insertByBo(ProSubjectTempBo bo) {
        ProSubjectTemp add = BeanUtil.toBean(bo, ProSubjectTemp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSubjectId(add.getSubjectId());
        }
        return flag;
    }

    /**
     * 修改项目管理模板
     */
    @Override
    public Boolean updateByBo(ProSubjectTempBo bo) {
        ProSubjectTemp update = BeanUtil.toBean(bo, ProSubjectTemp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProSubjectTemp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目管理模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    private ArrayList<SysUser> parseUsersBynames(String headUser){
        ArrayList<SysUser> headUsers = new ArrayList<>();
        if(StringUtils.isNotBlank(headUser)){
            String[] split = headUser.split(",");
            for (String username : split) {
                SysUser sysUser = sysUserService.selectUserByUserName(username);
                if(ObjectUtils.isNotNull(sysUser)){
                    headUsers.add(sysUser);
                }
            }
        }
        return headUsers;
    }
}
